import 'package:cashbook/custom/SimpleBarChart.dart';
import 'package:flutter/material.dart';
import 'package:charts_flutter/flutter.dart' as charts;

class SimpleSeriesLegend extends StatelessWidget {
  List<charts.Series> seriesList;
  var _seriesList = [];
  bool animate;

  SimpleSeriesLegend(this._seriesList, {this.animate});

  @override
  Widget build(BuildContext context) {
    this.seriesList = _createSampleData();
    this.animate = true;
    return new charts.BarChart(
      seriesList,
      animate: animate,
      barGroupingType: charts.BarGroupingType.grouped,
      // Add the series legend behavior to the chart to turn on series legends.
      // By default the legend will display above the chart.
      behaviors: [new charts.SeriesLegend()],
    );
  }

  //数据转换
  List<charts.Series<OrdinalSales, String>> _createSampleData() {
    if (_seriesList.length != 2) {
      return [];
    }

    List item0 = _seriesList[0];
    List item1 = _seriesList[1];

    for (int i = 0; i < item0.length; i++) {
      OrdinalSales sales0 = item0[i];
      bool flag = item1.contains(sales0);
      if(!flag){
        OrdinalSales tmp = OrdinalSales(sales0.year,0);
        item1.insert(i, tmp);
      }
    }

    for (int i = 0; i < item1.length; i++) {
      OrdinalSales sales1 = item1[i];
      bool flag = item0.contains(sales1);
      if(!flag){
        OrdinalSales tmp = OrdinalSales(sales1.year,0);
        item0.insert(i, tmp);
      }
    }

    return [
      new charts.Series<OrdinalSales, String>(
        id: '收入',
        domainFn: (OrdinalSales sales, _) => sales.year,
        measureFn: (OrdinalSales sales, _) => sales.sales,
        data: _seriesList[0],
      ),
      new charts.Series<OrdinalSales, String>(
        id: '支出',
        domainFn: (OrdinalSales sales, _) => sales.year,
        measureFn: (OrdinalSales sales, _) => sales.sales,
        data: _seriesList[1],
      ),
    ];
  }
}
